package com.xsm.ppsecret.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xsm.ppsecret.R;
import com.xsm.ppsecret.model.LogEntry;

import java.util.List;

/**
 * Author: Xiasem
 * Email: xiasem@163.com
 * Date: 17-4-7.
 */

public class LogAdapter extends BaseRecyclerAdapter implements View.OnClickListener, View.OnLongClickListener {

    private Context mContext;
    private List<LogEntry> mList;

    public LogAdapter(Context context, List<LogEntry> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_log_layout, parent, false);
        itemView.setOnClickListener(this);
        itemView.setOnLongClickListener(this);
        return new LogHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof LogHolder) {
            LogHolder logHolder = (LogHolder) holder;
            logHolder.bindView(mList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return mList.size() > 0 ? mList.size() : 0;
    }

    @Override
    public void onClick(View v) {
        super.adapterClick(v);
    }

    @Override
    public boolean onLongClick(View v) {
        super.adapterLongClick(v);
        return true;
    }

    private static class LogHolder extends RecyclerView.ViewHolder{

        private TextView content;

        public LogHolder(View itemView) {
            super(itemView);
            content = (TextView) itemView.findViewById(R.id.item_log_text);
        }

        public void bindView(LogEntry logEntry) {
            String string = logEntry.getContent();
            if (string != null) {
                this.content.setText(string);
            } else {
                this.content.setText("");
            }
        }

    }
}
